<template>
  <el-dialog
    v-model="visible"
    width="1200px"
    :close-on-click-modal="false"
    append-to-body
    class="unified-dialog detail-dialog-tabs"
  >
    <template #header>
      <DialogHeader
        icon="Document"
        title="标签页详情示例"
        description="使用 el-tabs 分组展示详情信息"
        type="primary"
      />
    </template>
    
    <div v-if="data" class="dialog-content">
      <el-tabs type="border-card">
        <!-- 基本信息 -->
        <el-tab-pane label="基本信息">
          <el-descriptions :column="2" border>
            <el-descriptions-item label="ID">{{ data.id }}</el-descriptions-item>
            <el-descriptions-item label="配置ID">{{ data.config_id }}</el-descriptions-item>
            <el-descriptions-item label="Topic名称">
              <el-tag type="primary" size="small">{{ data.topic_name }}</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="分区ID">{{ data.partition_id }}</el-descriptions-item>
            <el-descriptions-item label="最新Offset">{{ data.latest_offset }}</el-descriptions-item>
            <el-descriptions-item label="消费滞后">
              <el-tag :type="data.consumer_lag > 1000 ? 'danger' : 'warning'" size="small">
                {{ data.consumer_lag }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="Topic大小">{{ formatBytes(data.topic_size_bytes) }}</el-descriptions-item>
            <el-descriptions-item label="消息Key">{{ data.message_key || '-' }}</el-descriptions-item>
            <el-descriptions-item label="消费者组">{{ data.consumer_group || '-' }}</el-descriptions-item>
            <el-descriptions-item label="Broker地址">{{ data.broker_address || '-' }}</el-descriptions-item>
          </el-descriptions>
        </el-tab-pane>
        
        <!-- 监控配置 -->
        <el-tab-pane label="监控配置">
          <el-descriptions :column="2" border>
            <el-descriptions-item label="监控状态">
              <el-tag :type="data.is_monitored ? 'success' : 'info'" size="small">
                {{ data.is_monitored ? '已启用' : '已禁用' }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="告警启用">
              <el-tag :type="data.is_alert_enabled ? 'success' : 'info'" size="small">
                {{ data.is_alert_enabled ? '已启用' : '已禁用' }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="告警阈值">{{ data.alert_threshold_hours }} 小时</el-descriptions-item>
            <el-descriptions-item label="最大告警次数">{{ data.max_alert_count }}</el-descriptions-item>
            <el-descriptions-item label="首次告警时间">{{ data.alert_first_send_time }}</el-descriptions-item>
            <el-descriptions-item label="第2次告警间隔">{{ data.alert_second_interval }} 秒</el-descriptions-item>
            <el-descriptions-item label="第3次告警间隔">{{ data.alert_third_interval }} 秒</el-descriptions-item>
            <el-descriptions-item label="Webhook URL" :span="2">
              <span class="text-muted">{{ data.webhook_url || '-' }}</span>
            </el-descriptions-item>
            <el-descriptions-item label="告警通知方式">
              <el-tag size="small" type="info">企业微信</el-tag>
              <el-tag size="small" type="info" style="margin-left: 8px;">邮件</el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="告警接收人">{{ data.alert_receivers || '-' }}</el-descriptions-item>
          </el-descriptions>
        </el-tab-pane>
        
        <!-- 状态信息 -->
        <el-tab-pane label="状态信息">
          <el-descriptions :column="2" border>
            <el-descriptions-item label="状态">
              <el-tag :type="getStatusType(data.status)" size="small">
                {{ getStatusText(data.status) }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="告警状态">
              <el-tag :type="getAlertStatusType(data.alert_status)" size="small">
                {{ getAlertStatusText(data.alert_status) }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="告警次数">{{ data.alert_count }}</el-descriptions-item>
            <el-descriptions-item label="告警历史ID">{{ data.alert_history_id || '-' }}</el-descriptions-item>
            <el-descriptions-item label="首次告警时间">{{ data.first_alert_time || '-' }}</el-descriptions-item>
            <el-descriptions-item label="最后告警时间">{{ data.last_alert_time || '-' }}</el-descriptions-item>
            <el-descriptions-item label="最近一次检查时间">{{ data.last_check_time || '-' }}</el-descriptions-item>
            <el-descriptions-item label="下次检查时间">{{ data.next_check_time || '-' }}</el-descriptions-item>
          </el-descriptions>
        </el-tab-pane>
        
        <!-- 时间信息 -->
        <el-tab-pane label="时间信息">
          <el-descriptions :column="2" border>
            <el-descriptions-item label="最新消息时间">{{ data.message_timestamp || '-' }}</el-descriptions-item>
            <el-descriptions-item label="创建时间">{{ data.created_at || '-' }}</el-descriptions-item>
            <el-descriptions-item label="更新时间">{{ data.updated_at || '-' }}</el-descriptions-item>
            <el-descriptions-item label="最后同步时间">{{ data.last_sync_time || '-' }}</el-descriptions-item>
            <el-descriptions-item label="配置生效时间">{{ data.config_effective_time || '-' }}</el-descriptions-item>
            <el-descriptions-item label="配置失效时间">{{ data.config_expire_time || '-' }}</el-descriptions-item>
          </el-descriptions>
        </el-tab-pane>
        
        <!-- 扩展信息 -->
        <el-tab-pane label="扩展信息">
          <el-descriptions :column="2" border>
            <el-descriptions-item label="环境">{{ data.environment || 'production' }}</el-descriptions-item>
            <el-descriptions-item label="集群名称">{{ data.cluster_name || '-' }}</el-descriptions-item>
            <el-descriptions-item label="备注" :span="2">{{ data.remark || '-' }}</el-descriptions-item>
            <el-descriptions-item label="标签" :span="2">
              <el-tag v-if="data.tags" v-for="tag in (typeof data.tags === 'string' ? data.tags.split(',') : data.tags)" :key="tag" size="small" style="margin-right: 8px;">
                {{ tag }}
              </el-tag>
              <span v-else>-</span>
            </el-descriptions-item>
            <el-descriptions-item label="元数据" :span="2">
              <pre v-if="data.metadata" class="metadata-content">{{ formatJsonString(data.metadata) }}</pre>
              <span v-else>-</span>
            </el-descriptions-item>
          </el-descriptions>
        </el-tab-pane>
      </el-tabs>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { computed } from 'vue'
import DialogHeader from '@/components/DialogHeader.vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['update:modelValue'])

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const handleClose = () => {
  visible.value = false
}

const formatBytes = (bytes) => {
  if (!bytes) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i]
}

const formatJsonString = (value, indent = 2) => {
  if (!value) return '-'
  
  try {
    if (typeof value === 'string') {
      try {
        const parsed = JSON.parse(value)
        return JSON.stringify(parsed, null, indent)
      } catch (e) {
        return value
      }
    }
    return JSON.stringify(value, null, indent)
  } catch (error) {
    return typeof value === 'string' ? value : String(value)
  }
}

const getStatusType = (status) => {
  const typeMap = {
    'active': 'success',
    'inactive': 'info',
    'monitored_only': 'warning'
  }
  return typeMap[status] || 'info'
}

const getStatusText = (status) => {
  const textMap = {
    'active': '活跃',
    'inactive': '非活跃',
    'monitored_only': '仅监控'
  }
  return textMap[status] || status
}

const getAlertStatusType = (status) => {
  const typeMap = {
    'normal': 'success',
    'alerting': 'danger'
  }
  return typeMap[status] || 'info'
}

const getAlertStatusText = (status) => {
  const textMap = {
    'normal': '正常',
    'alerting': '告警中'
  }
  return textMap[status] || status
}
</script>

<style scoped>
.text-muted {
  color: #909399;
}

.metadata-content {
  margin: 8px 0 0 0;
  padding: 12px;
  background-color: #f5f7fa;
  border-radius: 4px;
  font-family: 'Courier New', 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 12px;
  line-height: 1.6;
  max-height: 300px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-all;
  color: #303133;
}
</style>

